package com.rs.util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.poi.ss.usermodel.Workbook;

import com.rs.model.Perforsc;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

/**
 * 
 * 描述：操作excel的工具类
 * 
 * @author：fjc
 * @date 2020年7月29日
 * @version:1.0
 */
public class ExcelUtil {
	/**
	 * 
	 * @MethodName: excelDownload
	 * @Description: excel下载
	 * @author：fjc
	 * @date 2020-07-30 10:14:20 
	 * @param list
	 * @return String
	 */
	public static String excelDownload(List<Perforsc> list, String title, String sheetName, String path) {
		// 生成workbook 并导出

		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, sheetName), Perforsc.class, list);
		File savefile = new File(path);
		File fileParent = savefile.getParentFile();
		if (!fileParent.exists()) {
			fileParent.mkdirs();
		}
		//先删除所有的文件
		if (savefile.isDirectory()) {
			File[] files = savefile.listFiles();
			for (File f : files) {
				f.delete();
			}
		}
		FileOutputStream fos;
		try {
			//创建文件
			savefile.createNewFile();
			fos = new FileOutputStream(path);
			workbook.write(fos);
			fos.close();
			System.out.println("path:" + path);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return FileOperationsUtil.getFileBase64(path);
	}
}
